<template>
  <view class="user-details-container">

    <!-- 用户基本信息卡片 -->
    <view class="user-card">
      <view class="user-avatar">
        <uni-icons type="person-filled" size="70" color="#898989"></uni-icons>
      </view>
      <view class="user-basic-info">
        <text class="user-name">{{ userInfo.nickname }}</text>
        <text class="user-id">ID: {{ userInfo.id || userInfo.userId || "" }}</text>
        <text class="user-role">角色: {{ userInfo.roleType === '0' ? '管理员' : '普通用户' }}</text>
        <text class="user-join-time">加入时间: {{ formatDate(userInfo.createdAt || userInfo.createTime) }}</text>
      </view>
    </view>

    <!-- 用户数据卡片 -->
    <view class="card" v-if="roleType == '1'">
      <view class="card-header">
        <text class="card-title">用户信息</text>
      </view>
      <view class="profile-content">
        <view class="profile-item">
          <text class="profile-label">累计答题</text>
          <text class="profile-value">{{ userProfile.totalQuestion || 0 }}</text>
        </view>
        <view class="profile-item">
          <text class="profile-label">总积分</text>
          <text class="profile-value">{{ userProfile.totalScore || 0 }}</text>
        </view>
        <view class="profile-item">
          <text class="profile-label">正确率</text>
          <text class="profile-value">{{ formatRate(userProfile.correctRate) }}</text>
        </view>
        <view class="profile-item">
          <text class="profile-label">对战胜场</text>
          <text class="profile-value">{{ userProfile.battleWin || 0 }}</text>
        </view>
      </view>
    </view>

    <!-- 排名信息卡片 -->
    <view class="card" v-if="roleType == '1'">
      <view class="card-header">
        <text class="card-title">排名信息</text>
      </view>
      <view class="rank-content">
        <view class="rank-item">
          <text class="rank-label">当前排名</text>
          <text class="rank-value">{{ rankData.rank || '暂无排名' }}</text>
        </view>
        <view class="rank-item">
          <text class="rank-label">总用户数</text>
          <text class="rank-value">{{ rankData.total || 0 }}</text>
        </view>
      </view>
    </view>

    <!-- 个人信息卡片 -->
    <view class="card">
      <view class="card-header">
        <text class="card-title">个人信息</text>
      </view>
      <view class="personal-info-content">
        <view class="info-item">
          <text class="info-label">邮箱</text>
          <text class="info-value">{{ userInfo.email || "未设置" }}</text>
        </view>
        <view class="info-item">
          <text class="info-label">昵称</text>
          <text class="info-value">{{ userInfo.nickname || "未设置" }}</text>
        </view>
        <view class="info-item">
          <text class="info-label">签名</text>
          <text class="info-value">{{ userInfo.signature || "这个人很懒，什么都没留下" }}</text>
        </view>
      </view>
    </view>

    <!-- 加载中提示 -->
    <view v-if="isLoading" class="loading">
      <uni-load-more status="loading" iconType="snow"></uni-load-more>
    </view>
  </view>
</template>

<script setup>
import { ref, computed, onMounted } from "vue";
import { getUserRank, getUserInfo } from "@/api/user";
import { onLoad } from "@dcloudio/uni-app";


// 用户信息
const userInfo = ref({});
const userProfile = ref({});
const rankData = ref({});
const isLoading = ref(true);

// 从路由参数获取用户ID
const userId = ref("");
const roleType = ref("");

// 格式化百分比
const formatRate = (rate) => {
  if (!rate && rate !== 0) return '0.00%';
  return `${rate}%`;
};

// 格式化日期
const formatDate = (dateStr) => {
  if (!dateStr) return "未知";
  const date = new Date(dateStr);
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`;
};

// 获取普通用户详情和排名数据
const fetchUserDetails = async (id) => {
  isLoading.value = true;
  try {
    const res = await getUserRank({userId: id});
    if (res.code === 200 && res.data) {
      rankData.value = res.data;
      userProfile.value = res.data.user || {};
      userInfo.value = {
        ...res.data.user,
        ...userInfo.value
      };
    } else {
      uni.showToast({
        title: res.msg || "获取用户信息失败",
        icon: "none",
      });
    }
  } catch (error) {
    console.error("获取用户信息失败:", error);
    uni.showToast({
      title: "获取用户信息失败",
      icon: "none",
    });
  } finally {
    isLoading.value = false;
  }
};

// 获取管理员用户详情
const fetchAdminDetails = async (id) => {
  isLoading.value = true;
  try {
    const res = await getUserInfo(id);
    if (res.code === 200 && res.data) {
      userInfo.value = res.data;
    } else {
      uni.showToast({
        title: res.msg || "获取管理员信息失败",
        icon: "none",
      });
    }
  } catch (error) {
    console.error("获取管理员信息失败:", error);
    uni.showToast({
      title: "获取管理员信息失败",
      icon: "none",
    });
  } finally {
    isLoading.value = false;
  }
};

// 返回上一页
const goBack = () => {
  uni.navigateBack();
};

// 页面加载时获取参数
onLoad((option) => {
  console.log(option);
  if (option && option.id) {
    userId.value = option.id;
    roleType.value = option.roleType;
    if(roleType.value == '1') {
      fetchUserDetails(option.id);
    } else if(roleType.value == '0') {
      fetchAdminDetails(option.id);
    } else {
      isLoading.value = false;
    }
  }
});
</script>

<style lang="scss" scoped>
.user-details-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 20rpx;
}

.back-button {
  display: flex;
  align-items: center;
  padding: 20rpx 0;
  margin-bottom: 20rpx;

  text {
    margin-left: 10rpx;
    font-size: 28rpx;
  }
}

.user-card {
  background-color: #fff;
  border-radius: 12rpx;
  padding: 40rpx;
  display: flex;
  align-items: center;
  margin-bottom: 30rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
}

.user-avatar {
  width: 150rpx;
  height: 150rpx;
  border-radius: 50%;
  overflow: hidden;
  margin-right: 30rpx;

  image {
    width: 100%;
    height: 100%;
  }
}

.user-basic-info {
  display: flex;
  flex-direction: column;
}

.user-name {
  font-size: 36rpx;
  font-weight: bold;
  margin-bottom: 10rpx;
}

.user-id,
.user-role,
.user-join-time {
  font-size: 24rpx;
  color: #666;
  margin-bottom: 8rpx;
}

.card {
  background-color: #fff;
  border-radius: 12rpx;
  padding: 20rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.card-header {
  margin-bottom: 16rpx;
  border-bottom: 1px solid #f0f0f0;
  padding-bottom: 10rpx;

  .card-title {
    font-size: 28rpx;
    font-weight: bold;
    color: #333;
  }
}

.profile-content, .rank-content {
  padding: 10rpx 0;

  .profile-item, .rank-item {
    display: flex;
    justify-content: space-between;
    margin-bottom: 16rpx;

    .profile-label, .rank-label {
      font-size: 26rpx;
      color: #666;
    }

    .profile-value, .rank-value {
      font-size: 26rpx;
      color: #333;
      font-weight: bold;
    }
  }
}

.personal-info-content {
  .info-item {
    display: flex;
    justify-content: space-between;
    padding: 15rpx 0;
    border-bottom: 1px solid #f0f0f0;

    &:last-child {
      border-bottom: none;
    }

    .info-label {
      font-size: 26rpx;
      color: #666;
    }

    .info-value {
      font-size: 26rpx;
      color: #333;
    }
  }
}

.loading {
  padding: 40rpx 0;
  display: flex;
  justify-content: center;
}
</style>
